{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "lesbian-springer",
   "metadata": {},
   "source": [
    "## Seldon V2 Experiment Version Tests\n",
    "\n",
    "This notebook will show how we can update running experiments."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amber-inclusion",
   "metadata": {},
   "source": [
    "### Test change candidate for a model\n",
    "\n",
    "We will use three SKlearn Iris classification models to illustrate experiment updates."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "359b456e",
   "metadata": {},
   "source": [
    "Load all models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "elect-samba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/sklearn1.yaml\n",
    "!seldon model load -f ./models/sklearn2.yaml\n",
    "!seldon model load -f ./models/sklearn3.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "floral-moisture",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model status iris -w ModelAvailable\n",
    "!seldon model status iris2 -w ModelAvailable\n",
    "!seldon model status iris3 -w ModelAvailable"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14a99e65",
   "metadata": {},
   "source": [
    "Let's call all three models individually first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0a7cfc57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "570c4769",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris2 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a102ca45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris3_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris3 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89efd99a",
   "metadata": {},
   "source": [
    "We will start an experiment to change the iris endpoint to split traffic with the `iris2` model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "10fe4447",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris\r\n",
      "  candidates:\r\n",
      "  - name: iris\r\n",
      "    weight: 50\r\n",
      "  - name: iris2\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/ab-default-model.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "differential-array",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment start -f ./experiments/ab-default-model.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "overhead-banks",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"experimentName\": \"experiment-sample\",\r\n",
      "  \"active\": true,\r\n",
      "  \"candidatesReady\": true,\r\n",
      "  \"mirrorReady\": true,\r\n",
      "  \"statusDescription\": \"experiment active\",\r\n",
      "  \"kubernetesMeta\": {}\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment status experiment-sample -w | jq -M ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05cdc618",
   "metadata": {},
   "source": [
    "Now when we call the iris model we should see a roughly 50/50 split between the two models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "finite-significance",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::48 :iris_1::52]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 100 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1c7ed7",
   "metadata": {},
   "source": [
    "Now we update the experiment to change to a split with the `iris3` model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2f614d4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris\r\n",
      "  candidates:\r\n",
      "  - name: iris\r\n",
      "    weight: 50\r\n",
      "  - name: iris3\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/ab-default-model2.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "03aada00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment start -f ./experiments/ab-default-model2.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "64e0ac04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"experimentName\": \"experiment-sample\",\r\n",
      "  \"active\": true,\r\n",
      "  \"candidatesReady\": true,\r\n",
      "  \"mirrorReady\": true,\r\n",
      "  \"statusDescription\": \"experiment active\",\r\n",
      "  \"kubernetesMeta\": {}\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment status experiment-sample -w | jq -M ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56fbcbf4",
   "metadata": {},
   "source": [
    "Now we should see a split with the `iris3` model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "73e85d3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris3_1::42 :iris_1::58]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 100 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "downtown-construction",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment stop experiment-sample"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51ef922f",
   "metadata": {},
   "source": [
    "Now the experiment has been stopped we check everything as before."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8ca25a85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d6c20826",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris2 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a695dd79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris3_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris3 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "077bbce8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload iris\n",
    "!seldon model unload iris2\n",
    "!seldon model unload iris3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdd680a2",
   "metadata": {},
   "source": [
    "### Test change default model in an experiment\n",
    "\n",
    "Here we test changing the model we want to split traffic on. We will use three SKlearn Iris classification models to illustrate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0c477529",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/sklearn1.yaml\n",
    "!seldon model load -f ./models/sklearn2.yaml\n",
    "!seldon model load -f ./models/sklearn3.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "440a0751",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model status iris -w ModelAvailable\n",
    "!seldon model status iris2 -w ModelAvailable\n",
    "!seldon model status iris3 -w ModelAvailable"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8e89353",
   "metadata": {},
   "source": [
    "Let's call all three models to verify initial conditions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "acebcac3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2c58faba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris2 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5d0ebaa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris3_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris3 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a42a568e",
   "metadata": {},
   "source": [
    "Now we start an experiment to change calls to the `iris` model to split with the `iris2` model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6ed52032",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris\r\n",
      "  candidates:\r\n",
      "  - name: iris\r\n",
      "    weight: 50\r\n",
      "  - name: iris2\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/ab-default-model.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "92c1044f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment start -f ./experiments/ab-default-model.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4499a50c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"experimentName\": \"experiment-sample\",\r\n",
      "  \"active\": true,\r\n",
      "  \"candidatesReady\": true,\r\n",
      "  \"mirrorReady\": true,\r\n",
      "  \"statusDescription\": \"experiment active\",\r\n",
      "  \"kubernetesMeta\": {}\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment status experiment-sample -w | jq -M ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc376cb",
   "metadata": {},
   "source": [
    "Run a set of calls and record which route the traffic took. There should be roughly a 50/50 split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f09e5168",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::51 :iris_1::49]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 100 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb0d3927",
   "metadata": {},
   "source": [
    "Now let's change the model we want to experiment to modify to the `iris3` model. Splitting between that and `iris2`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c26e67ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Experiment\r\n",
      "metadata:\r\n",
      "  name: experiment-sample\r\n",
      "spec:\r\n",
      "  default: iris3\r\n",
      "  candidates:\r\n",
      "  - name: iris3\r\n",
      "    weight: 50\r\n",
      "  - name: iris2\r\n",
      "    weight: 50\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./experiments/ab-default-model3.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "71484348",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment start -f ./experiments/ab-default-model3.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "82c0da83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"experimentName\": \"experiment-sample\",\r\n",
      "  \"active\": true,\r\n",
      "  \"candidatesReady\": true,\r\n",
      "  \"mirrorReady\": true,\r\n",
      "  \"statusDescription\": \"experiment active\",\r\n",
      "  \"kubernetesMeta\": {}\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment status experiment-sample -w | jq -M ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b1768bb",
   "metadata": {},
   "source": [
    "Let's check the iris model is now as before but the iris3 model has traffic split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "112c92dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "07033575",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::25 :iris3_1::25]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris3 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "29ae8890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris2 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d4aced42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon experiment stop experiment-sample"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0efd0a45",
   "metadata": {},
   "source": [
    "Finally, let's check now the experiment has stopped as is as at the start."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "832aab89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "2b846b96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris2_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris2 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a239c6ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: map[:iris3_1::50]\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris3 -i 50 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d0fe31cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload iris\n",
    "!seldon model unload iris2\n",
    "!seldon model unload iris3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2aec7ac8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
